﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;
using LeHouse.Server.WebApi.Common;


namespace LeHouse.Server.WebApi.Controllers
{
    public class RoleController : ApiController
    {
        /// <summary>
        /// 根据角色名称显示角色信息集
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Role/Select")]
        public List<System_Role> showRole([FromBody]string roleName)
        {
            System_RoleService showrole = new System_RoleService();
            if (string.IsNullOrEmpty(roleName))
            {
                return showrole.Select();
            }
            return showrole.Select(p => p.T_Rname.Contains(roleName.Trim()));
        }
        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="ro"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Role/Insert")]
        public bool Insert([FromBody]System_Role ro)
        {
            ro.T_Rstate = Convert.ToInt32(EnumState.StateOK);
            ro.T_IsSystem = 1;
            System_RoleService inser = new System_RoleService();
            if (inser.Insert(ro) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除角色信心
        /// </summary>
        /// <param name="id">根据编号删除角色信息</param>
        /// <returns>true/false</returns>
        [HttpPost]
        [Route("api/Role/Delete")]
        public bool deleerole([FromBody]int id)
        {
            System_RoleService delect = new System_RoleService();
            if (delect.DeleteById(id) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据编号id获取角色的信息
        /// </summary>
        /// <param name="id">获取所选中编号的信息</param>
        /// <returns>true/false</returns>
        [HttpPost]
        [Route("api/Role/UpdateByid")]
        public System_Role UpdateByid([FromBody]int id)
        {
            System_RoleService role = new System_RoleService();
            return role.SelectById(id);

        }
        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roe">获取已修改的角色信息</param>
        /// <returns>true/false</returns>
        [HttpPost]
        [Route("api/Role/Update")]
        public bool Update([FromBody] System_Role roe)
        {
            System_RoleService updat = new System_RoleService();
            System_Role sr = updat.SelectById(roe.T_RoleID);
            if (sr == null)
                return false;
            sr.T_Rname = roe.T_Rname;
            sr.T_Remark = roe.T_Remark;
            if (updat.Update(sr) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据id修改角色状态
        /// </summary>
        /// <param name="dep">获取已修改的信息</param>
        /// <returns>true/false</returns>
        [HttpPost]
        [Route("api/Role/UpdateState")]
        public bool UpdateState(System_Role obj)
        {
            System_RoleService update = new System_RoleService();
            var d1 = update.SelectById(Convert.ToInt32(obj.T_RoleID));
            d1.T_Rstate = obj.T_Rstate;
            if (update.Update(d1) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
